using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web.UI.Design;

using PHSRAG.Insight21.CDS;
using PHSRAG.Insight21.Policy;
using PHSRAG.Insight21.ClinicalTrials;

namespace PHSRAG.Insight21.InsightWebControls.ClinicalTrials
{
   /// <summary>
   /// The Budget Expense Control is used to provide a user interface for a single Budget Expense.
   /// </summary>
   [ToolboxData("<{0}:BudgetVarCostControl runat=server></{0}:BudgetVarCostControl>"),Designer(typeof(BudgetVarCostControlDesigner))]
   public class BudgetVarCostControl : System.Web.UI.Control, INamingContainer
   {
      #region Instance Variables
      private BudgetExpense budgetVarCost;
      private TableRow tr;
      private TableCell tdDescription;
      private TableCell tdCost;
      private TableCell tdDelete;
      private CheckBox applyOverhead;
      private CheckBox delete;
      private TextBox inputDescription;
      private TextBox inputCost;
      private RegularExpressionValidator costFormatValidator;
      private RequiredFieldValidator costRequiredValidator;
      private RequiredFieldValidator descriptionValidator;
      #endregion

      #region Public Properties
      /// <summary>
      /// Apply overhead indicator
      /// </summary>
      public bool ApplyOverhead
      {
         get
         {
            return applyOverhead.Checked;
         }
      }

      /// <summary>
      /// Delete indicator
      /// </summary>
      public bool Delete
      {
         get
         {
            return delete.Checked;
         }
      }

      /// <summary>
      /// Cost Description
      /// </summary>
      public string CostDescription
      {
         get
         {
            return inputDescription.Text;
         }
      }

      /// <summary>
      /// Cost Amount
      /// </summary>
      public string CostAmount
      {
         get
         {
            return inputCost.Text;
         }
      }
      #endregion
   
      #region Constructor
      /// <summary>
      /// Sets the private Budget Expense member from the passed in parameter
      /// </summary>
      /// <param name="budgetVarCost"></param>
      public BudgetVarCostControl( BudgetExpense budgetVarCost )
      {
         this.budgetVarCost = budgetVarCost;
      }
      #endregion

      #region Overrides
      /// <summary>
      /// Runs upon loading of the control
      /// </summary>
      /// <param name="e"></param>
      protected override void OnLoad(EventArgs e)
      {
         base.OnLoad(e);
         EnsureChildControls( );
      }

      /// <summary>
      /// Creates all child controls, applying styles, setting values, and binding validation
      /// </summary>
      protected override void CreateChildControls( )
      {
         inputDescription = new TextBox( );
         inputDescription.ID = "inputDescription";
         inputCost = new TextBox( );
         inputCost.ID = "inputCost";
         inputCost.Text = decimal.Parse( budgetVarCost.ExpenseCost ).ToString( "F" );

         applyOverhead = new CheckBox( );
         applyOverhead.Checked = budgetVarCost.ApplyOverhead;

         delete = new CheckBox( );

         inputDescription.CssClass = "ExpandControlWithCheckbox";
         inputDescription.Text = budgetVarCost.StudyCostCode;
         
         costFormatValidator = new RegularExpressionValidator( );
         costFormatValidator.ErrorMessage = "Cost must be a valid dollar amount.";
         costFormatValidator.Display = ValidatorDisplay.None;
         costFormatValidator.ControlToValidate = inputCost.ID;
         costFormatValidator.ValidationExpression = @"\d+\.{0,1}\d{0,2}";

         costRequiredValidator = new RequiredFieldValidator( );
         costRequiredValidator.ErrorMessage = "Cost is required.";
         costRequiredValidator.Display = ValidatorDisplay.None;
         costRequiredValidator.ControlToValidate = inputCost.ID;

         descriptionValidator = new RequiredFieldValidator( );
         descriptionValidator.ErrorMessage = "Description is required.";
         descriptionValidator.Display = ValidatorDisplay.None;
         descriptionValidator.ControlToValidate = inputDescription.ID;

         tr = new TableRow( );
         tdDescription = new TableCell( );
         tdDescription.Controls.Add( descriptionValidator );
         tdDescription.Controls.Add( applyOverhead );
         tdDescription.Controls.Add( inputDescription );

         tdCost = new TableCell( );
         tdCost.Controls.Add( costFormatValidator );
         tdCost.Controls.Add( costRequiredValidator );
         tdCost.Controls.Add( inputCost );
         
         tdDelete = new TableCell( );
         tdDelete.Controls.Add( delete );

         tr.Controls.Add( tdDescription );
         tr.Controls.Add( tdCost );
         tr.Controls.Add( tdDelete );

         Controls.Add( tr );
      }

      /// <summary>
      /// Used to disable the delete checkbox if the task has not been saved
      /// </summary>
      /// <param name="e"></param>
      protected override void OnPreRender(EventArgs e)
      {
         delete.Enabled = ( budgetVarCost.Id != 0 );

         // Format numbers entered by the user
         if ( !String.IsNullOrEmpty(inputCost.Text) )
            inputCost.Text = decimal.Parse( inputCost.Text ).ToString( "f" );

         base.OnPreRender (e);
      }

      #endregion

   }

   #region Class BudgetVarCostControlDesigner
   /// <summary>
   /// Designer associated with the BudgetVarCostControl control.
   /// </summary>
   /// <remarks>
   /// This class cannot be used directly.
   /// </remarks>
   public class BudgetVarCostControlDesigner : ControlDesigner
   {
      #region Overrides
      /// <summary>
      /// Since the BudgetVarCostControl control does not have any design-time visibility, the designer provides
      /// the type name of that control and the name of the instance.
      /// </summary>
      /// <returns>HTML representation of the BudgetVarCostControl control</returns>
      public override string GetDesignTimeHtml( )
      {
         return CreatePlaceHolderDesignTimeHtml( );
      }
      #endregion
   }
   #endregion
}
